#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/10/29 22:25
# @Author  : Nabby
# @File    : user_impl.py.py
# @Desc    :



from app import db
from infrastructure.po.mode.user import User
from infrastructure.po.mode.user_role import UserRole

class UserImpl(object):
    @classmethod
    def get_user_list(cls, username=None, country=None, company_name=None, email=None, phone=None, page=1, page_size=999):
        query = db.session.query(User)
        if username:
            query = query.filter(
                User.username.like(f"%{username}%")
            )
        if country:
            query = query.filter(
                User.country.like(f"%{country}%")
            )
        if company_name:
            query = query.filter(
                User.company_name.like(f"%{company_name}%")
            )
        if email:
            query = query.filter(
                User.email.like(f"%{email}%")
            )
        if phone:
            query = query.filter(
                User.phone.like(f"%{phone}%")
            )
        return query.paginate(page=page, per_page=page_size), query.count()



    @classmethod
    def get_user_by_id(cls, user_id):
        user = db.session.query(User).filter(User.id == user_id).first()
        return user

    @classmethod
    def get_company_name(cls, user_id):
        """
        获取用户公司名称
        :param user_id:
        :return:
        """
        user = db.session.query(User).filter(User.id == user_id).first()
        return user.company_name.strip()

    @classmethod
    def get_user_by_name(cls, name):
        user = db.session.query(User).filter(User.username == name).first()
        return user

    @classmethod
    def add_user(cls, id, username, password, country, companyName, email, phone, role_id):
        count = db.session.query(User).filter(
            User.username == username
        ).count()
        if count == 0:
            user = User(
                id=id,
                username=username,
                password=password,
                country=country,
                company_name=companyName,
                email=email,
                phone=phone,
                status=1
            )
            db.session.add(user)
            db.session.flush()
            db.session.commit()

            #插入角色
            user_role = UserRole(
                user_id=user.id,
                role_id=role_id
            )
            db.session.add(user_role)
            db.session.flush()
            db.session.commit()
            return True
        else:
            return False

    @classmethod
    def update_user(
            cls,
            id,
            username,
            role_id,
            country,
            companyName,
            phone,
            email
    ):
        count = db.session.query(User).filter(
            User.username == username,
            User.id != id,
            User.status != 1
        ).count()
        if count == 0:
            user_data = {
                "id": id,
                "username": username,
                "country": country,
                "phone": phone,
                "email": email,
                "country": country,
                "company_name": companyName
            }
            db.session.query(User).filter(User.id == id).update(user_data)
            db.session.commit()

            # 此处逻辑为先删除关联表下用户的所有角色数据，之后插入对应用户的角色
            db.session.query(UserRole).filter(UserRole.user_id == id).delete()

            # 插入角色
            user_role = UserRole(
                user_id=id,
                role_id=role_id
            )
            db.session.add(user_role)
            db.session.flush()
            db.session.commit()
            return True
        else:
            return False

    @classmethod
    def update_user_password(
            cls,
            user_id,
            password
    ):
        db.session.query(User).filter(User.id == user_id).update(
            {
                "password": password
            }
        )
        db.session.commit()
        return True

    @classmethod
    def delete_user(
            cls,
            user_id
    ):
        db.session.query(UserRole).filter(UserRole.user_id == user_id).delete()
        db.session.query(User).filter(User.id == user_id).delete()
        db.session.commit()
        return True

    @classmethod
    def change_user_status(
            cls,
            user_id,
            status
    ):
        db.session.query(User).filter(User.id == user_id).update({
            "status": status,
        })
        db.session.commit()
        return True

    @classmethod
    def get_user_role(
            cls,
            user_id
    ):
        user_role = db.session.query(UserRole).filter(UserRole.user_id == user_id).first()
        return user_role


